package com.lzy.imagepickerdemo.zn;

import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.lzy.imagepickerdemo.GApp;
import com.lzy.imagepickerdemo.R;
import com.lzy.imagepickerdemo.imageloader.GlideImageLoader;
import com.lzy.imagepickerdemo.zn.adapter.PhotoTypeAdapter;
import com.lzy.imagepickerdemo.zn.db.PhotoListEntity;
import com.lzy.imagepickerdemo.zn.db.PhotoTypeEntity;
import com.lzy.imagepickerdemo.zn.recyle.WrapContentLinearLayoutManager;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class LookSeeActivity extends ImageGridActivity {

    RecyclerView revPhotoList;
    PhotoTypeAdapter mPhotoTypeAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        revPhotoList=(RecyclerView)findViewById(R.id.rev_photo_list);
        findViewById(R.id.btn_preview).setVisibility(View.GONE);
        //revPhotoList.setLayoutManager(new LinearLayoutManager(this, RecyclerView.LayoutManager.H));
        revPhotoList.setLayoutManager(new WrapContentLinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));

        mPhotoTypeAdapter=new PhotoTypeAdapter();
        revPhotoList.setAdapter(mPhotoTypeAdapter);
        findViewById(R.id.top_bar).setVisibility(View.GONE);
        findViewById(R.id.lin_top).setVisibility(View.VISIBLE);
        findViewById(R.id.tv_serach).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LookSeeActivity.this,SearchActivity.class));
            }
        });
        findViewById(R.id.tv_resut).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*开始分析*/
                startActivity(new Intent(LookSeeActivity.this,AddActivity.class));
                //mImageFolders.get(0).images
            }
        });

        mPhotoTypeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                PhotoTypeEntity mPhotoTypeEntity= mPhotoTypeAdapter.getData().get(position);
                PhotoListActivity.start(LookSeeActivity.this,mPhotoTypeEntity.photo_type,mPhotoTypeEntity.name);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == ImagePicker.REQUEST_CODE_TAKE) {
            List<String> mCaList=new ArrayList<>();
            try {
                for (PhotoTypeEntity MPhotoTypeEntity: mPhotoTypeAdapter.getData()){
                    List<PhotoListEntity> list= GApp.getInstance().mDBManager.getPhotolist(MPhotoTypeEntity.photo_type);
                    if (list.size()>0){
                        mCaList.add(list.get(0).img_url);
                    }else{
                        mCaList.add(null);
                    }
                }
                /*拍照返回的照片与相册的照片准个比较*/
                for (int i=0;i<mCaList.size();i++){
                    String locationUrl=imagePicker.getTakeImageFile().getAbsolutePath();
                    String name=imagePicker.getTakeImageFile().getName();
                    String upUrl=mCaList.get(i);
                    /*判断是否是同一个人*/
                    post(new File(locationUrl),new File(upUrl),i,locationUrl,name);

                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    private  void post(File file1,File file2,final int postion,final String locationUrl,final String name){
        OkGo.<String>post("https://api-cn.faceplusplus.com/facepp/v3/compare").tag(this).isMultipart(true)
                .params("api_key","vqerz9mXvV2ZcvG7urZ09Yq3anLYzqi3")
                .params("api_secret","k-JE9XO_JYtaXBROOjcgsaGTEVdd05ql")
                .params("image_file1",file1)
                .params("image_file2",file2)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Log.i("tt","返回结果->>"+response.body().toString());
                        try {
                            JSONObject jsonObject=new JSONObject(response.body().toString());
                            if (jsonObject.getDouble("confidence")>50){
                                addPhoto(mPhotoTypeAdapter.getData().get(postion),locationUrl,name);
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }

                    }
                });

    }

    private void addPhoto(PhotoTypeEntity MPhotoTypeEntity,String locationUrl,String name){
        ContentValues values = new ContentValues();
        values.put("id",MPhotoTypeEntity.photo_type);
        values.put("photo_type",MPhotoTypeEntity.photo_type);
        values.put("img_id","");
        values.put("img_url",locationUrl);
        values.put("img_name",name);
        GApp.getInstance().mDBManager.addPhotoList(values);
    }
    @Override
    protected void onResume() {
        super.onResume();
        ImagePicker.getInstance().setShowCamera(true);
        ImagePicker.getInstance().setCrop(false);
        ImagePicker.getInstance().isCheckShow=false;
        ImagePicker.getInstance().setImageLoader(new GlideImageLoader());
        mPhotoTypeAdapter.setNewData(GApp.getInstance().mDBManager.getPhotoType());
    }
}
